import { CommonModal } from "@/views/components/CommonModal";
import { message, Form } from "antd";
import CommonSelect from "@/pages/OrderModule/common/components/CommonSelect";
import { CommonForm } from "@/pages/OrderModule/common/components/CommonForm";
import { getGenerateRulesConfig, saveGenerateRulesConfig } from "../../../service";
import { useEffect, useState } from "react";
const options = [
	{ label: "按原始编码生成", value: "original-code" },
	{ label: "按店铺款式编码+颜色规格", value: "spu-spec" },
	{ label: "按原始编码+颜色规格", value: "original-code-spec" },
	{ label: "按原始编码+平台商品id", value: "original-code-id" }
];
const connectOptions = [
	{ label: "-", value: "-" },
	{ label: "/", value: "/" },
	{ label: "无连接符", value: "无连接符" }
];
const AutomaticGenerateSetModal = ({ onCancel }) => {
	const [form] = Form.useForm();
	const [data, setData] = useState({});
	const onOk = () => {
		form.validateFields().then(values => {
			const { connector, generateRule } = form.getFieldsValue();
			const API_DATA = { connector: connector == "无连接符" ? "" : connector, generateRule };
			saveGenerateRulesConfig(API_DATA).then(res => {
				const { success } = res;
				if (success) {
					message.success("操作成功");
					onCancel();
				}
			});
		});
	};
	useEffect(() => {
		form.setFieldsValue({
			...data,
			connector: data.connector == "" ? "无连接符" : data.connector
		});
	}, [data]);
	useEffect(() => {
		getGenerateRulesConfig().then(res => {
			const { success, data } = res;
			if (success && data) setData(data);
		});
	}, []);
	return (
		<CommonModal title={{ title: "自动生成系统商品规则" }} open={true} width="468px" onOk={onOk} onCancel={onCancel}>
			<CommonForm
				formProps={{
					form,
					initialValues: {},
					onFinish: onOk
				}}
				columns={[
					{
						name: "generateRule",
						Component: CommonSelect,
						rules: [{ required: true, message: "必选，选择生成规则" }],
						props: {
							options: options,
							placeholder: "必选，选择生成规则"
						}
					},
					{
						name: "connector",
						Component: CommonSelect,
						rules: [{ required: true, message: "必选，选择连接符" }],
						props: {
							options: connectOptions,
							placeholder: "必选，选择连接符"
						}
					}
				]}
			/>
		</CommonModal>
	);
};
export default AutomaticGenerateSetModal;
